package com.coincalf.wallet.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.coincalf.dto.CoinInfoDto;
import com.coincalf.feign.CoinServiceClient;
import com.coincalf.framework.utils.MnemonicService;
import com.coincalf.wallet.blockchain.BlockConstant;
import com.coincalf.wallet.blockchain.util.AESUtil;
import com.coincalf.wallet.entity.AddressPool;
import com.coincalf.wallet.mapper.AddressPoolMapper;
import com.coincalf.wallet.service.AddressPoolService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jcajce.provider.symmetric.AES;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户的地址池 服务实现类
 * </p>
 *
 * @author crow
 * @since 2018-05-16
 */
@Slf4j
@Service
public class AddressPoolServiceImpl extends ServiceImpl<AddressPoolMapper, AddressPool> implements AddressPoolService {


    @Autowired
    private CoinServiceClient coinServiceClient;

    /**
     * 根据币种ID获取钱包地址
     *
     * @param coinId 币种ID
     * @return
     */
    @Override
    public AddressPool selectAddress(Long coinId) {
        EntityWrapper<AddressPool> wrapper = new EntityWrapper<>();
        wrapper.eq("coin_id", coinId).last("LIMIT 1");
        return super.selectOne(wrapper);
    }

    @Override
    public int addAddress(String coinName, Integer amount) {
        int success = 0;
        if (StringUtils.isBlank(coinName)) {
            //不传则所有币种均创建地址
            if (amount == null || amount < 1) {
                amount = 1000;
            }

            //获取所有主链币种 注意：这里面包括了EOS和XRP
            List<CoinInfoDto> mainCoins = coinServiceClient.selectAllMainCoin();

            for (int i = 0; i < amount; i++) {
                for (CoinInfoDto coinInfo : mainCoins) {
                    MnemonicService.WalletResponse wallet = this.initAddress(coinInfo.getCoinName());
                    this.getSuccess(coinInfo, wallet);
                    success++;
                }
            }
        } else {
            CoinInfoDto coinInfo = coinServiceClient.selectCoinInfo(coinName);
            if (coinInfo != null) {
                if (coinInfo.isMain()) {
                    if (amount == null || amount < 1) {
                        amount = 1000;
                    }
                    for (int i = 0; i < amount; i++) {
                        MnemonicService.WalletResponse wallet = this.initAddress(coinName);
                        this.getSuccess(coinInfo, wallet);
                        success++;
                    }
                } else {
                    log.warn("非主链币种 不增加地址 coinName={}", coinName);
                    return 0;
                }
            }
        }
        return success;
    }

    public void getSuccess(CoinInfoDto coinInfo, MnemonicService.WalletResponse wallet) {
        if (wallet != null) {
            AddressPool addressPool = new AddressPool();
            addressPool.setAddress(wallet.getAddress());
            addressPool.setCoinId(coinInfo.getCoinId());
            addressPool.setPrivateKey(AESUtil.encrypt(wallet.getPrivateKey(), BlockConstant.AES_KEY));
            super.insert(addressPool);
        }
    }

    private MnemonicService.WalletResponse initAddress(String coinName) {
        switch (coinName) {
            case "BTC":
                return MnemonicService.getBtcAddress(MnemonicService.generateMnemonicWords(), 0, 1);
            case "ETH":
                return MnemonicService.getEthAddress(MnemonicService.generateMnemonicWords(), 0);
            case "LTC":
                return MnemonicService.getLtcAddress(MnemonicService.generateMnemonicWords(), 0);
            case "TRX":
                return MnemonicService.getTrxAddress(MnemonicService.generateMnemonicWords(), 0, 1);
            default:
                return null;
        }
    }


    public static void main(String[] args) {
        //MnemonicService.WalletResponse wallet = MnemonicService.getEthAddress(MnemonicService.generateMnemonicWords(), 0);
        //System.out.println("50c982e92b3e47fed75e4db4968bcfe312125d5f2a666cd7e5bc5b2a6ec29269");//0xf6dc90bfcfae03e6a92c7e1c00e6bcbced6e9fdb
        String encrypt = AESUtil.encrypt("0b441c679a5b756e6f7e78e6e45edb63dd0c6acd030b54f54fc77462919bd9c6", BlockConstant.AES_KEY);
        System.out.println(encrypt);
        System.out.println(AESUtil.decrypt("9F61BA6891683DE78725EE23E83EE0E6F4F06C4DBE385325E17C46D4F45FDE94B31886DBDA6FAE7223E2CBDC0A3B09AE53795B0CD149D9C94E7852715884F4F21AAC60F54F5DDC2B4C6F24B6C4B545C3", BlockConstant.AES_KEY));
        System.out.println("8b7a82aec0981fbb7a913727589b9169703f6e8f4a4f26421b9318bdf844740".length());
    }

}
